package cn.tedu.collection;/**
 * @Author 金帮嘉诚 @Date 2021-06-23 星期三 19:52
 */
/*本类用于测试List接口*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 知识点
 * 从Collection继承的方法
 * <p>
 * 自身特有的方法：
 * 单个List：
 * add 加
 * get 获取下标处元素
 * indexOf 判断元素下标，第一个
 * lastIndexOf 判断元素下标，最后一个
 * remove 移除遇到的第一个
 * set 会改变下标处的值为输入的参数值
 * sublist 不会改变原List
 */
public class TestList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("胡坦");
        list.add("陈雅倩");
        list.add("魏天南");
        list.add("路梦蝶");
        list.add("王子末");
        list.add("Lena Anderson");
        list.add("Kim");
        System.out.println("list = " + list);
        System.out.println("list.contains(\"Kim\") = " + list.contains("Kim"));
        System.out.println("list.equals(\"Kim\") = " + list.equals("Kim"));//判断
        System.out.println("list.hashCode() = " + list.hashCode());
        System.out.println("list.isEmpty = " + list.isEmpty());
        System.out.println("list.size() = " + list.size());
        Object[] array = list.toArray();
        System.out.println("Arrays.toString(array) = " + Arrays.toString(array));

        System.out.println("-------------------------------list特有操作----------------------------------");
        //add get indexOf lastIndexOf
        System.out.println("list = " + list);
        list.add("胡坦");
        System.out.println("list = " + list);
        list.add(1, "胡坦");
        System.out.println("list = " + list);
        list.add(5, "胡坦");
        System.out.println("list = " + list);
        list.add(7, "胡坦");
        System.out.println("list = " + list);
        System.out.println("list.get(2) = " + list.get(2));
        System.out.println("list.indexOf(\"胡坦\") = " + list.indexOf("胡坦"));
        System.out.println("list.lastIndexOf(\"胡坦\") = " + list.lastIndexOf("胡坦"));

        //remove set sublist
        System.out.println("list = " + list);
        System.out.println("list = \"胡坦\" " + list.remove("胡坦"));
        System.out.println("list = " + list);
        System.out.println("list.set(6,\"本田岬\") = " + list.set(6, "本田岬"));
        System.out.println("list = " + list);
        System.out.println("list.subList(1,7) = " + list.subList(1, 7));//不会改变原List

        System.out.println("----------------------------集合间操作------------------------------------");
        //addAll
        //removeAll
        //containAll
        List<String> list1 = new ArrayList<>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("4");
        System.out.println("list.addAll(list1) = " + list.addAll(list1));//末尾加入
        System.out.println("list = " + list);
        System.out.println("list.addAll(2,list1) = " + list.addAll(2, list1));//从下标2开始插入
        System.out.println("list = " + list);
        System.out.println("list.retainAll(list1) = " + list.retainAll(list1));//差集list-list1
        System.out.println("list = " + list);
        System.out.println("list.removeAll(list1) = " + list.removeAll(list1));//存在相同元素就删，不管多少个
        System.out.println("list = " + list);


    }
}
